package com.honeycomb.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.honeycomb.db.entity.MapInfo;
import com.honeycomb.db.entity.UserInfo;
import com.honeycomb.db.entity.UserPassword;
import com.honeycomb.db.entity.UserStatus;
import com.honeycomb.service.BaseService;
import com.honeycomb.service.IUserService;
import com.honeycomb.utils.HoneyCombColumnIndicator;
import com.honeycomb.utils.HoneyCombTools;
import com.honeycomb.vo.BaseVO;
import com.honeycomb.vo.MapInfoVO;
import com.honeycomb.vo.ResultDto;
import com.honeycomb.vo.UserInfoVO;
import com.honeycomb.vo.UserPasswordVO;
import com.honeycomb.vo.UserRegistVO;
import com.honeycomb.vo.UserStatusVO;

public class UserServiceImpl extends BaseService implements IUserService {

	public void dotest() {
		List<UserInfo> list = getiUserinfoDao().findAll();
		for (int i = 0; i < list.size(); i++) {
			UserInfo u = list.get(i);
			logger.info("=============user id============="+u.getUserId());
		}
	}

	/**
	 * 
	 * @author ZhongJiang
	 * 
	 * @function 用户注册Service方法
	 * 
	 * @param userRegistVO 用户注册信息VO
	 * 
	 * @param mapInfoVO 用户地理信息VO
	 * 
	 * @return {ResultDto<BaseVO>}
	 * 
	 */
	@Override
	public ResultDto<BaseVO> doRegist(UserRegistVO userRegistVO, MapInfoVO mapInfoVO) {
		logger.info("Hello - UserServiceImpl - doRegist...");
		ResultDto resultDto = new ResultDto<BaseVO>();
		MapInfo mapInfo = null;
		UserInfo userInfo = null;
		UserPassword userPassword = null;
		try {
			mapInfo = new MapInfo();
			mapInfo.setMapId(mapInfoVO.getMapId());
			mapInfo.setMapProvider(mapInfoVO.getMapProvider());
			mapInfo.setMapX(mapInfoVO.getMapX());
			mapInfo.setMapY(mapInfoVO.getMapY());
			mapInfo.setMapAddr(mapInfoVO.getMapAddr());
			mapInfo.setMapDesc(mapInfoVO.getMapDesc());
			mapInfo.setMapCreateDttm(mapInfoVO.getMapCreateDttm());
			mapInfo.setMapCreateIp(mapInfoVO.getMapCreateIp());
			mapInfo.setMapType(mapInfoVO.getMapType());
			this.getiMapinfoDao().add(mapInfo);
			
			userInfo = new UserInfo();
			userPassword = new UserPassword();
			userInfo.setMapInfo(mapInfo);
			userInfo.setUserId(userRegistVO.getUserEmail());
			userInfo.setUserNickname(userRegistVO.getUserNickname());
			userInfo.setUserSex(userRegistVO.getUserSex().charAt(0));
			userInfo.setUserRegistDttm(HoneyCombTools.convertCurrentDTTMtoInt());
			userInfo.setUserRegistIp(userRegistVO.getUserRegistIp());
			userInfo.setUserHeadImgUrl(userRegistVO.getUserHeadImgUrl());
			userInfo.setUserBirthday(HoneyCombTools.convertDTtoInt(HoneyCombTools.formatStringToDateDT(userRegistVO.getUserBirthday())));
			this.getiUserinfoDao().add(userInfo);
			
			userPassword.setUserId(userInfo.getUserId());
			userPassword.setUserInfo(userInfo);
			userPassword.setPasswordValue(userRegistVO.getUserPassword());
			this.getiUserPasswordDao().add(userPassword);
			
			resultDto.setSuccess(true);
			return resultDto;
		} catch (Exception e) {
			e.printStackTrace();
			resultDto.setSuccess(false);
			return resultDto;
		}
	}

	/**
	 * @author ZhongJiang
	 * @create 2012-10-23
	 * @function 用户登录Service方法
	 */
	@Override
	public ResultDto<UserInfoVO> doLogin(UserPasswordVO userPasswordVO) {
		logger.info("Hello - UserServiceImpl - doLogin...");
		ResultDto<UserInfoVO> resultDto = new ResultDto<UserInfoVO>();
		List<UserPassword> userPasswordList = null;
		UserInfo userInfo = null;
		UserInfoVO userInfoVO = null;
		try {
			userPasswordList = this.getiUserPasswordDao().findUserPasswordInfo(userPasswordVO.getUserId(),userPasswordVO.getPasswordValue());
			if(userPasswordList != null && userPasswordList.size() != 0) {
				//查出UserInfo信息,用户session存值
				userInfo = this.getiUserinfoDao().findById(userPasswordVO.getUserId());
				userInfoVO = new UserInfoVO();
				userInfoVO.setUserId(userInfo.getUserId());
				userInfoVO.setUserNickname(userInfo.getUserNickname());
				userInfoVO.setUserSex(userInfo.getUserSex().toString());
				userInfoVO.setUserRegistDttmStr(HoneyCombTools.formatDTTM(HoneyCombTools.convertIntToDTTM(userInfo.getUserRegistDttm())));
				userInfoVO.setUserHeadImgUrl(userInfo.getUserHeadImgUrl());
				userInfoVO.setUserBirthday(HoneyCombTools.formatDT(HoneyCombTools.convertIntToDT(userInfo.getUserBirthday())));
				userInfoVO.setUserRegistIp(userInfo.getUserRegistIp());
				userInfoVO.setMapX(userInfo.getMapInfo().getMapX());
				userInfoVO.setMapY(userInfo.getMapInfo().getMapY());
				resultDto.addToResultList(userInfoVO);
				resultDto.setSuccess(true);
			}else {
				resultDto.setSuccess(false);
			}
			return resultDto;
		} catch (Exception e) {
			e.printStackTrace();
			resultDto.setSuccess(false);
			return resultDto;
		}

	}

	/**
	 * @author ZhongJiang
	 * @create 2012-10-23
	 * @function 获取UserInfo用户信息
	 * @param userInfoId 用户ID
	 */
	@Override
	public ResultDto<BaseVO> doQueryUserInfo(String userInfoId) {
		logger.info("Hello - UserServiceImpl - doQueryUserInfo...");
		ResultDto<BaseVO> resultDtoQueryUserInfo = new ResultDto<BaseVO>();
		UserInfoVO resultVO = new UserInfoVO();
		UserInfo userInfo = null;
		try {
			userInfo = this.getiUserinfoDao().findById(userInfoId);
			if(userInfo != null) {
				resultVO.setUserId(userInfo.getUserId());
				resultVO.setUserNickname(userInfo.getUserNickname());
				resultVO.setUserHeadImgUrl(userInfo.getUserHeadImgUrl());
				resultVO.setUserSex(userInfo.getUserSex().toString());
				resultDtoQueryUserInfo.addToResultList(resultVO);
				resultDtoQueryUserInfo.setSuccess(true);
			}else {
				resultDtoQueryUserInfo.setSuccess(false);
			}
			return resultDtoQueryUserInfo;
		} catch (Exception e) {
			e.printStackTrace();
			resultDtoQueryUserInfo.setSuccess(false);
			return resultDtoQueryUserInfo;
		}
	}

	/**
	 * @author ZhongJiang
	 * @create 2012-10-25
	 * @function 更新UserInfo用户基本信息
	 * @param userInfoVO 用户基本信息更新表
	 */
	@Override
	public ResultDto<BaseVO> doUpdateUserInfo(UserInfoVO userInfoVO) {
		logger.info("Hello - UserServiceImpl - doUpdateUserInfo...");
		ResultDto<BaseVO> resultDtoUpdateUserInfo = new ResultDto<BaseVO>();
		UserInfo userInfo = new UserInfo();
		try {
			userInfo = this.getiUserinfoDao().findById(userInfoVO.getUserId());
			userInfo.setUserNickname(userInfoVO.getUserNickname());
			userInfo.setUserSex(userInfoVO.getUserSex().charAt(0));
			this.getiUserinfoDao().update(userInfo);
			resultDtoUpdateUserInfo.setSuccess(true);
			return resultDtoUpdateUserInfo;
		} catch (Exception e) {
			e.printStackTrace();
			resultDtoUpdateUserInfo.setSuccess(false);
			return resultDtoUpdateUserInfo;
		}
	}

	/**
	 * @author ZhongJiang
	 * @create 2012-10-25
	 * @function 更新UserPassword用户密码
	 * @param userPasswordVO 用户密码表
	 */
	@Override
	public ResultDto<BaseVO> doUpdateUserPassword(UserPasswordVO userPasswordVO) {
		logger.info("Hello - UserServiceImpl - doUpdateUserPassword...");
		ResultDto<BaseVO> resultDto = new ResultDto<BaseVO>();
		List<UserPassword> userPasswordList = null;
		UserPassword userPassword = null;
		try {
			userPasswordList = this.getiUserPasswordDao().findUserPasswordInfo(userPasswordVO.getUserId(),userPasswordVO.getPasswordValue());
			if(userPasswordList != null && userPasswordList.size() != 0) {
				userPassword = userPasswordList.get(0);
				userPassword.setPasswordValue(userPasswordVO.getNewPasswordValue());
				this.getiUserPasswordDao().update(userPassword);
				resultDto.setSuccess(true);
			}else {
				resultDto.setSuccess(false);
			}
		} catch (Exception e) {
			e.printStackTrace();
			resultDto.setSuccess(false);
		}
		return resultDto;
	}

	/**
	 * @author ZhongJiang
	 * @create 2012-10-26
	 * @function 检查用户ID是否已存在
	 * @param userInfoVO.userId 用户ID
	 */
	@Override
	public ResultDto<BaseVO> doCheckUserRegistEmailExist(UserInfoVO userInfoVO) {
		logger.info("Hello - UserServiceImpl - doCheckUserRegistEmailExist...");
		ResultDto<BaseVO> resultDtoCheckEmailExist = new ResultDto<BaseVO>();
		UserInfo userInfo = null;
		try {
			userInfo = this.getiUserinfoDao().findById(userInfoVO.getUserId());
			//如果userInfo不存在，返回false
			if(userInfo == null) {
				resultDtoCheckEmailExist.setSuccess(false);
			}else {
				resultDtoCheckEmailExist.setSuccess(true);
			}
			return resultDtoCheckEmailExist;
		} catch (Exception e) {
			e.printStackTrace();
			resultDtoCheckEmailExist.setSuccess(false);
			return resultDtoCheckEmailExist;
		}
	}

	/**
	 * @author ZhongJiang
	 * @create 2012-10-29
	 * @function 更新用户登录状态Status
	 * @premise 用户登录信息正确，并且登录
	 * @param userStatusVO.userId 用户ID
	 */
	@Override
	public ResultDto<BaseVO> doUpdateUserStatusToOnline(UserStatusVO userStatusVO) {
		logger.info("Hello - UserServiceImpl - doUpdateUserStatusToOnline...");
		ResultDto<BaseVO> resultDtoUpdateLoginStatus = new ResultDto<BaseVO>();
		UserStatus userStatus = null;
		try {
			userStatus = this.getiUserStatusDao().findById(userStatusVO.getUserId());
			if(userStatus == null) {
				userStatus = new UserStatus();
				UserInfo userInfo = this.getiUserinfoDao().findById(userStatusVO.getUserId());
				userStatus.setUserId(userStatusVO.getUserId());
				userStatus.setStatusValue(HoneyCombColumnIndicator.USER_LOGIN_STATUS_IS_ONLINE);
				userStatus.setLoginDttm(HoneyCombTools.convertCurrentDTTMtoInt());
				userStatus.setUserInfo(userInfo);
				this.getiUserStatusDao().add(userStatus);
				resultDtoUpdateLoginStatus.setSuccess(true);
			}else {
				if(userStatus.getStatusValue() != HoneyCombColumnIndicator.USER_LOGIN_STATUS_IS_ONLINE) {
					userStatus.setStatusValue(HoneyCombColumnIndicator.USER_LOGIN_STATUS_IS_ONLINE);
					userStatus.setLoginDttm(HoneyCombTools.convertCurrentDTTMtoInt());
					this.getiUserStatusDao().update(userStatus);
				}
				resultDtoUpdateLoginStatus.setSuccess(true);
			}
			return resultDtoUpdateLoginStatus;
		} catch (Exception e) {
			e.printStackTrace();
			resultDtoUpdateLoginStatus.setSuccess(false);
			return resultDtoUpdateLoginStatus;
		}
	}

	@Override
	public ResultDto<BaseVO> doUpdateUserStatusToNotOnline(UserStatusVO userStatusVO) {
		logger.info("Hello - UserServiceImpl - doUpdateUserStatusToNotOnline...");
		ResultDto<BaseVO> resultDtoUpdateLoginStatus = new ResultDto<BaseVO>();
		UserStatus userStatus = null;
		try {
			userStatus = this.getiUserStatusDao().findById(userStatusVO.getUserId());
			if(userStatus == null) {
				resultDtoUpdateLoginStatus.setSuccess(false);
			}else {
				if(userStatus.getStatusValue() == HoneyCombColumnIndicator.USER_LOGIN_STATUS_IS_ONLINE) {
					userStatus.setStatusValue(HoneyCombColumnIndicator.USER_LOGIN_STATUS_IS_NOT_ONLINE);
					this.getiUserStatusDao().update(userStatus);
				}
				resultDtoUpdateLoginStatus.setSuccess(true);
			}
			return resultDtoUpdateLoginStatus;
		} catch (Exception e) {
			e.printStackTrace();
			resultDtoUpdateLoginStatus.setSuccess(false);
			return resultDtoUpdateLoginStatus;
		}
	}

}











